﻿/*
    Copyright (C) 2008, wj2929, wj2929@gmail.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    *KEEP THIS HEADER INFORMATION*
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mjRegister.Entity;

using System.IO;
using System.Xml.Linq;
using System.Xml;

namespace mjRegister.BAL
{
    //车市
    public class CarMarket
    {
        //写入车市数据到文件，作为下次读取之缓存。
        public static void Init(IList<CarMarketInfo> carMarketInfos)
        {
            string carMarketFile = GetCarMarketFile();
            string carMarketTempFile = GetCarMarketTempFile();

            if (File.Exists(carMarketTempFile))
                File.Delete(carMarketTempFile);

            XElement xmlCarMarkets = new XElement("CarMarkets");
            for (int j = 0; j < carMarketInfos.Count; j++)
            {
                CarMarketInfo carMarketInfo = carMarketInfos[j];
                xmlCarMarkets.Add(new XElement("CarMarket",
                    new XElement("carid", carMarketInfo.CarId.ToString()),
                    new XElement("carname", carMarketInfo.CarName),
                    new XElement("carprice", carMarketInfo.CarPrice.ToString())
                    )
                );
            }
            using (XmlTextWriter writer = new XmlTextWriter(carMarketTempFile, Encoding.UTF8))
                xmlCarMarkets.WriteTo(writer);

            if (File.Exists(carMarketFile))
                File.Delete(carMarketFile);

            File.Move(carMarketTempFile, carMarketFile);
        }

        //从文件中获取车市数据
        public static IList<CarMarketInfo> GetCarMarketInfos()
        {
            IList<CarMarketInfo> carMarketInfos = new List<CarMarketInfo>();

            if (File.Exists(GetCarMarketFile()))
            {
                XDocument xmlCarMarket = XDocument.Load(GetCarMarketFile());

                var xmlCarMarkes = xmlCarMarket.Root.Elements("CarMarket");

                int index = 0;
                foreach (XElement xnCarMarke in xmlCarMarkes)
                {
                    carMarketInfos.Add(new CarMarketInfo
                    {
                        CarId = int.Parse(xnCarMarke.Element("carid").Value),
                        CarName = xnCarMarke.Element("carname").Value,
                        CarPrice = int.Parse(xnCarMarke.Element("carprice").Value),
                        Index = index++
                    });
                }
            }
            return carMarketInfos;
        }

        private static string GetCarMarketFile()
        {
            return Path.Combine(Common.GetConfigPath(), "carmarket.xml");
        }

        private static string GetCarMarketTempFile()
        {
            return Path.Combine(Common.GetConfigPath(), string.Format("{0}_carmarket_temp.xml",Guid.NewGuid().ToString()));
        }
    }
}
